package com.example.administrator.weather;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.tencent.bugly.crashreport.BuglyLog;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private IPAddressModle ipAddressModle;
    private weatherModle weatherModle;
    private TextView tvIp, tvCity, tvDate, tvWeather, tvWind, tvTemp;
    private TextView tvDate1, tvWeather1, tvWind1, tvTemp1;
    private TextView tvDate2, tvWeather2, tvWind2, tvTemp2;
    private List<String> ipList = new ArrayList<>();
    private List<String> wList = new ArrayList<>();
    private String ip = null;
    private String city = null;
    private String data, data1, data2 = null;
    private String weather, weather1, weather2 = null;
    private String wind, wind1, wind2 = null;
    private String temp, temp1, temp2 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ipAddressModle = new IPAddressModle();
        weatherModle = new weatherModle();
        inint();
        MyThread myThread = new MyThread();
        new Thread(myThread).start();
    }

    private void inint() {
        tvIp = (TextView) findViewById(R.id.tv_ip);
        tvCity = (TextView) findViewById(R.id.tv_city);
        tvDate = (TextView) findViewById(R.id.tv_date);
        tvWeather = (TextView) findViewById(R.id.tv_weather);
        tvWind = (TextView) findViewById(R.id.tv_wind);
        tvTemp = (TextView) findViewById(R.id.tv_temp);
        tvDate1 = (TextView) findViewById(R.id.tv_date1);
        tvWeather1 = (TextView) findViewById(R.id.tv_weather1);
        tvWind1 = (TextView) findViewById(R.id.tv_wind1);
        tvTemp1 = (TextView) findViewById(R.id.tv_temp1);
        tvDate2 = (TextView) findViewById(R.id.tv_date2);
        tvWeather2 = (TextView) findViewById(R.id.tv_weather2);
        tvWind2 = (TextView) findViewById(R.id.tv_wind2);
        tvTemp2 = (TextView) findViewById(R.id.tv_temp2);
    }

    class MyThread implements Runnable {
        @Override
        public void run() {
            ipList = ipAddressModle.getIPAddress();
            if (ipList.size() > 0) {
                city = ipList.get(0);
                ip = ipList.get(1);
                wList = weatherModle.getWeather(ip);

            }
            MainActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvIp.setText(ip);
                    tvCity.setText(city);
                    handler.sendEmptyMessage(0);
                }
            });
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (wList.size() > 0) {
                        data = wList.get(0);
                        weather = wList.get(1);
                        wind = wList.get(2);
                        temp = wList.get(3);
                        data1 = wList.get(4);
                        weather1 = wList.get(5);
                        wind1 = wList.get(6);
                        temp1 = wList.get(7);
                        data2 = wList.get(8);
                        weather2 = wList.get(9);
                        wind2 = wList.get(10);
                        temp2 = wList.get(11);
                    }
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                tvDate.setText(data);
                                tvWeather.setText(weather);
                                tvWind.setText(wind);
                                tvTemp.setText(temp);
                                tvDate1.setText(data1);
                                tvWeather1.setText(weather1);
                                tvWind1.setText(wind1);
                                tvTemp1.setText(temp1);
                                tvDate2.setText(data2);
                                tvWeather2.setText(weather2);
                                tvWind2.setText(wind2);
                                tvTemp2.setText(temp2);
                            } catch (Throwable throwable) {
                                CrashReport.postCatchedException(throwable);
                                BuglyLog.i("tag", temp2);
                            }
                        }
                    });
                    break;
            }
        }
    };
}
